package edu.sctu;


import edu.sctu.User;
import edu.sctu.UserDao;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * 用户列表Servlet
 */
public class UserListServlet extends HttpServlet {

    UserDao userDao = new UserDao();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session=request.getSession();
        //判断是否登录
        if (session.getAttribute("isLogin") == null){
            session.setAttribute("notLogin","请先登录！");
            response.sendRedirect("/login.jsp");//未登录则跳转到登录页面
        }else {
            userList(request,response);
        }


    }

    /**
     * 获取用户列表
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    private void userList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<User> userList = userDao.getUserList();
        request.setAttribute("userList",userList);
        //连接jsp和servlet的关键代码
        request.getRequestDispatcher("/userList.jsp").forward(request,response);
    }
}